package com.psl.services.work;

import com.psl.Dao.Conn;
import com.psl.model.halfproduct;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mioko on 2017/10/5.
 */
public class finishProductServices {
    public boolean InsertFinishProduct(String h_no, String h_name, String h_size, String h_r, String h_n, int h_num, String h_date, String h_people, String h_ticketno, int status) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("insert into proc_finishproduct(h_no,h_name,h_size,h_r,h_n,h_num,h_date,h_people,h_ticketno,status) VALUES (?,?,?,?,?,?,?,?,?,?)");
            pstmt.setString(1, h_no);
            pstmt.setString(2, h_name);
            pstmt.setString(3, h_size);
            pstmt.setString(4, h_r);
            pstmt.setString(5, h_n);
            pstmt.setInt(6, h_num);
            pstmt.setString(7, h_date);
            pstmt.setString(8, h_people);
            pstmt.setString(9, h_ticketno);
            pstmt.setInt(10, status);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }


    public List Allprint() {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List product = new ArrayList();
        String sql = "SELECT h_no,h_name,h_size,h_r,h_n,h_num,h_date,h_people,h_ticketno,status,h_id from proc_finishproduct";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                halfproduct p = new halfproduct();
                p.setH_no(rs.getString(1));
                p.setH_name(rs.getString(2));
                p.setH_size(rs.getString(3));
                p.setH_r(rs.getString(4));
                p.setH_n(rs.getString(5));
                p.setH_num(rs.getInt(6));
                p.setH_date(rs.getString(7));
                p.setH_people(rs.getString(8));
                p.setH_ticketno(rs.getString(9));
                p.setStatus(rs.getInt(10));
                p.setH_id(rs.getInt(11));
                product.add(p);
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("finish", product);
            }
            return product;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public List querywait() {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List product = new ArrayList();
        String sql = "SELECT h_no,h_name,h_size,h_r,h_n,h_num,h_date,h_people,h_ticketno,status,h_id from proc_finishproduct where status=2";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                halfproduct p = new halfproduct();
                p.setH_no(rs.getString(1));
                p.setH_name(rs.getString(2));
                p.setH_size(rs.getString(3));
                p.setH_r(rs.getString(4));
                p.setH_n(rs.getString(5));
                p.setH_num(rs.getInt(6));
                p.setH_date(rs.getString(7));
                p.setH_people(rs.getString(8));
                p.setH_ticketno(rs.getString(9));
                p.setStatus(rs.getInt(10));
                p.setH_id(rs.getInt(11));
                product.add(p);
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("wait", product);
                request.setAttribute("msg", "<a href='/editfpsback.do'>提示:您有" + product.size() + "条成品被退回，请检查后重新输入</a>");
            }
            return product;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
    }


    public boolean updatestatuson(int h_id) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("UPDATE proc_finishproduct set status=1 WHERE h_id=?");
            pstmt.setInt(1, h_id);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public boolean InsertIntoFinishKc(int h_id) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("INSERT INTO finishproduct_kc(h_no,h_size, h_r, h_n, h_num_now)(SELECT h_no,h_size, h_r, h_n, h_num FROM proc_finishproduct WHERE h_id = ?)");
            pstmt.setInt(1, h_id);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }







    public boolean updatestatusoff(int h_id) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("UPDATE proc_finishproduct set status=2 WHERE h_id=?");
            pstmt.setInt(1, h_id);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }


    public boolean editfpsback(int h_id, String h_no, String h_name, String h_size, String h_r, String h_n, int h_num, String h_date, String h_people, int status) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("UPDATE proc_finishproduct SET h_no=?,h_name=?,h_size=?,h_r=?,h_n=?,h_num=?,h_date=?,h_people=?,status=? WHERE h_id=?");
            pstmt.setString(1, h_no);
            pstmt.setString(2, h_name);
            pstmt.setString(3, h_size);
            pstmt.setString(4, h_r);
            pstmt.setString(5, h_n);
            pstmt.setInt(6, h_num);
            pstmt.setString(7, h_date);
            pstmt.setString(8, h_people);
            pstmt.setInt(9, status);
            pstmt.setInt(10, h_id);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }


    public boolean delproc(int h_id) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("DELETE from proc_finishproduct WHERE h_id=?");
            pstmt.setInt(1, h_id);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }


    public boolean updateprocstatus(String h_ticketno) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("UPDATE proc_finishproduct set h_ticketno=?");
            pstmt.setString(1, h_ticketno);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public List queryone(String h_id) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List product = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT h_no,h_name,h_size,h_r,h_n,h_num,h_date,h_people,h_ticketno,status,h_id from proc_finishproduct WHERE h_id=?");
            pstmt.setString(1, h_id);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                halfproduct p = new halfproduct();
                p.setH_no(rs.getString(1));
                p.setH_name(rs.getString(2));
                p.setH_size(rs.getString(3));
                p.setH_r(rs.getString(4));
                p.setH_n(rs.getString(5));
                p.setH_num(rs.getInt(6));
                p.setH_date(rs.getString(7));
                p.setH_people(rs.getString(8));
                p.setH_ticketno(rs.getString(9));
                p.setStatus(rs.getInt(10));
                p.setH_id(rs.getInt(11));
                product.add(p);
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("finish", product);
            }
            return product;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
